<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <!-- 新建角色按钮 -->
            <el-row style="height:60px">
              <el-col :span="24">
                <el-button size="small" type="primary" icon="el-icon-plus" @click="addRole">新建角色</el-button>
              </el-col>
            </el-row>
            <!-- 表格区域 -->
            <el-table :data="roleList" border>
              <el-table-column
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                prop="name"
                label="角色名"
                width="240"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="setRoles(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoles(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="block">
              <el-pagination
                :current-page="query.page"
                :page-sizes="[10, 15, 20, 25]"
                :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
            <!-- 点击出现的弹框 -->
            <el-dialog
              :title="isEdit?'编辑角色':'新增角色'"
              :visible.sync="showDialog"
              width="50%"
              :close-on-click-modal="false"
              :close-on-press-escape="false"
              @close="closeDialog"
            >
              <el-form v-if="showDialog" ref="form" :model="roleForm" :rules="rules" label-width="80px">
                <el-form-item label="活动名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="活动名称" prop="description">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitFn">确定</el-button>
                  <el-button @click="cancelFn">取消</el-button>
                </el-form-item>
              </el-form>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="公司信息" class="tab-pane">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 分配权限 -->
        <el-dialog
          title="分配权限"
          :visible.sync="setperDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <assign-permission
            v-if="setperDialog"
            :role-id="roleId"
            @close="setperDialog= false"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getAllrole, getcompanyInfo, addRolesapi, getRolesId, updateRole, delRole } from '@/api/setting'
import { mapGetters } from 'vuex'
import AssignPermission from './assignPermission.vue'
export default {
  name: 'Setting', // 给组件名字可以在v浏览器vue中找到到组件
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first',
      query: {
        page: 1,
        pagesize: 10
      }, // 控制分页,向结构传参
      roleList: [], // 角色列表
      total: 0, // 总数
      companyInfo: {}, // 公司信息
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      isEdit: false, // 编辑状态
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]

      },
      setperDialog: false,
      roleId: null
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    // 渲染列表
    this.getAllrole()
    // 获取公司信息
    this.getcompanyInfo()
  },
  methods: {
    setRoles(id) {
      this.setperDialog = true
      this.roleId = id
    },
    // 数据列表渲染
    async getAllrole() {
      const res = await getAllrole(this.query)
      this.roleList = res.data.rows
      this.total = res.data.total
    },

    // 编辑状态数据回填
    async editRoles(id) {
      // 调用接口获得信息
      const res = await getRolesId(id)
      // 将信息赋值给data里变量
      this.roleForm = res.data
      // 弹框显示
      this.showDialog = true
      // 点击编辑状态为true
      this.isEdit = true
    },

    // 删除功能
    async delRoles(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('已取消')
      const res = await delRole(id)
      this.$message.success(res.message)
      this.showDialog = false
      if (this.roleList.length === 1) {
        this.query.page--
        if (this.query.page === 1) {
          this.query.page = 1
        }
      }
      this.getAllrole()
    },

    // 每页显示多少
    handleSizeChange(newSize) {
      this.query.pagesize = newSize
      this.getAllrole()
    },

    // 当前页码
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getAllrole()
      // 如果删除第二页最后一个数据,跳回上一页
      // 在删除里面写
    },

    // 获取公司信息
    async getcompanyInfo() {
      const res = await getcompanyInfo(this.companyId)
      this.companyInfo = res.data
    },

    // 点击新增角色
    addRole() {
      this.showDialog = true
      this.isEdit = false
    },

    // 点击确定按钮
    submitFn() {
      // 先进行兜底验证
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // 给用户提示 关闭弹窗 渲染列表
        if (!this.isEdit) {
          const res = await addRolesapi(this.roleForm)
          this.$message.success(res.message)
        } else {
          const res = await updateRole(this.roleForm)
          this.$message.success(res.message)
        }
        this.getAllrole()
        this.showDialog = false
      })
    },

    // 点击取消按钮
    cancelFn() {
      this.showDialog = false
    },

    closeDialog() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}
form.el-form {
  padding: 0 40px 0 20px;
}
</style>
